package com.rp.system.dao;

import java.util.List;
import java.util.Properties;

import org.raptor.common.DbOperUtil;
import org.raptor.orm.hibernate.DbaHibernateTemplateSupport;
import org.raptor.pagination.Page2;
import org.springframework.stereotype.Repository;

import com.rp.common.utils.PropertiesLoader;
import com.rp.common.vo.PageParam;
import com.rp.system.po.Authority;

@Repository
public class AuthorityDao extends DbaHibernateTemplateSupport<Authority> {
	
	public static final String GET_AUTHORITIES = "from Authority";
	public static final String GET_AUTHORITIES_BY_ROLEID = "select elements(r.authorities) from Role r where r.id = ?";
	
	public static final String INSERT_AUTHORITY = "INSERT INTO RP_SYSTEM_AUTHORITIES " +
			"(id,createDate,updateDate,version,code,name,moduleId,moduleName,remark) " +
			"VALUES (?,?,?,?,?,?,?,?,?)";
	
	/**
	 * 获取超级权限，在数据库中，超级管理权限只会存在一个
	 * @return
	 */
	public Authority getSuperAuthority(){
		Properties properties = PropertiesLoader.loadProperties("rp.properties");
		String superAdminAuthCode = properties.getProperty("superAdminAuthCode");
		String hql = "from Authority where code = ?";
		List<Authority> auths = this.find(hql, superAdminAuthCode);
		if(auths.size() > 0){
			return auths.get(0);
		}
		return null;
	}
	
	public List<Authority> getAuthoritiesByResourceId(String resourceId){
		String hql = "select elements(r.authorities) from Resource as r where r.id = ?" + DbOperUtil.generateOrderBy(null, null);
		return this.find(hql, resourceId);
	}
	
	/**
	 * 根据角色ID获取角色对应的权限
	 * @param roleId
	 * @return
	 * @throws Exception 
	 */
	public Page2 getAuthoritiesByRoleId(PageParam pageParam, String roleId) throws Exception{
		Page2 page = null;
		if(roleId == null){
			throw new Exception("roleId不能为空");
		}else{
			page = this.getPage2ByOffset(GET_AUTHORITIES_BY_ROLEID, 
					pageParam.getPageOffset(), pageParam.getPageSize(),
					pageParam.getSortField(), pageParam.getSortOrder(), roleId);
		}
		
		return page;
	}
	
	/**
	 * 根据角色ID获取角色对应的权限，返回List列表
	 * @param roleId
	 * @return
	 * @throws Exception 
	 */
	public List<Authority> getAuthoritiesByRoleId(String roleId) throws Exception{
		if(roleId == null){
			throw new Exception("roleId不能为空");
		}
		return this.find(GET_AUTHORITIES_BY_ROLEID, roleId);
	}
	
	/*public List<Authority> getAuthoritiesByRoleId(String roleId){
		String hql = "from Authority where roles.id = ?" + DbOperUtil.generateOrderBy(null, null);
		return this.find(hql, roleId);
	}*/
	
	/**
	 * 分页获取权限数据
	 * @param pageParam
	 * @param authVo
	 * @return
	 */
	public Page2 getAuthoritiesByPage(PageParam pageParam, Authority auth){
		String hql = "from Authority where name like ? and code like ?";
		Page2 page = this.getPage2ByOffset(hql, 
				pageParam.getPageOffset(), pageParam.getPageSize(), 
				pageParam.getSortField(), pageParam.getSortOrder(),
				DbOperUtil.fuzzyQueryParam(auth.getName()),
				DbOperUtil.fuzzyQueryParam(auth.getCode()));
		return page;
	}
	
	/**
	 * 分页获取权限数据，传入角色ID，排除此角色拥有的权限
	 * @param pageParam
	 * @param roleId
	 * @return
	 */
	public Page2 getAuthoritiesByPage(PageParam pageParam, String roleId){
		List<Authority> hasAuths = this.find(GET_AUTHORITIES_BY_ROLEID, roleId);
		
		Page2 page = this.getPage2ByOffset(GET_AUTHORITIES, 
				pageParam.getPageOffset(), pageParam.getPageSize(),
				pageParam.getSortField(), pageParam.getSortOrder());
		
		List<Authority> allAuths = (List<Authority>) page.getData();
		for(int i=0; i<allAuths.size(); i++){
			if(hasAuths.contains(allAuths.get(i))){
				allAuths.remove(i);
				i--;
			}
		}
		page.setData(allAuths);
		return page;
	}
	
}
